<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\pay\index\controller;

use think\facade\View;
use app\index\BaseController;
/**
 * 生成二维码
 */
class Qrcode extends BaseController
{
    public function index()
    {
        require_once dirname(dirname(dirname ( __FILE__ ))).'/addons/wechat/example/phpqrcode/phpqrcode.php';
    	$url = urldecode($_GET["data"]);
    	if (isset($_GET['pay_type']) ) {
    		$path     = plugin_path() . 'pay/';
    		$logoName = $_GET['pay_type'] === 'wechat' ? 'pay-logo-wechat.png' : 'pay-logo-alipay.png';
    		$logoFile =	$path . $logoName;
    		$qrFile   = $path . 'qrcode.png';//已经生成的原始二维码图
    		\QRcode::png($url, $qrFile, 6, 6);
			$qrFile = imagecreatefromstring(file_get_contents($qrFile)); 
			$logoFile = imagecreatefromstring(file_get_contents($logoFile)); 
			$qrWidth = imagesx($qrFile);//二维码图片宽度 
			$qrHeight = imagesy($qrFile);//二维码图片高度 
			$logoWidth = imagesx($logoFile);//logo图片宽度 
			$logoHeight = imagesy($logoFile);//logo图片高度 
			$logoQrWidth = $qrWidth / 5; 
			$scale = $logoWidth/$logoQrWidth; 
			$logoQrheight = $logoHeight/$scale; 
			$fromwidth = ($qrWidth - $logoQrWidth) / 2; 
			//重新组合图片并调整大小 
			imagecopyresampled($qrFile, $logoFile, $fromwidth, $fromwidth, 0, 0, $logoQrWidth, 
			$logoQrheight, $logoWidth, $logoHeight); 
			//输出图片 
			Header("Content-type: image/png");
			ImagePng($qrFile);
    	} else {
    		\QRcode::png($url, false, 6, 6);
    	}
    }
}